﻿//Login.ascx.cs
////////////////////////////////////////
////////////////////////////////////////
//用户自定义控件，显示用户登录
////////////////////////////////////////
////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Login : System.Web.UI.UserControl
{
    CommonClass ccObj = new CommonClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {//当IsPostBack为false时候，表示第一次
            this.labValid.Text = ccObj.RandomNum(4);//产生随机验证码
        }
        if (Session["User"] != null)
        {
            Client user = (Client)Session["User"];
            if (user.roleID == 2) 
            { 
                labVIP.Visible = true; 
            }
            labUserName.Text = user.userName;
            if (user.lastLog == user.regTime)
            {
                labLastLog.Text = "这是您首次登录";
            }
            else
            {
                labLastLog.Text = "上次登录时间为：" + user.lastLog.ToString();
            }
            //判断用户是否登录
            this.tabafterlogin.Visible = true;  //显示用户欢迎界面
            this.tablogin.Visible = false;      //隐藏用户登录界面
        }
    }
    protected void Userlog_Click(object sender, EventArgs e)
    {
        //清空Session对象
        Session["User"] = null;
        if (this.Username.Value.Trim() == "")
        {
            Response.Write(ccObj.MessageBoxPage("登录名不能为空！"));
        }
        else if (this.Password.Value.Trim() == "")
        {
            Response.Write(ccObj.MessageBoxPage("密码不能为空！"));
        }
        else if (this.txtValid.Text.Trim() == "")
        {
            Response.Write(ccObj.MessageBoxPage("验证码不能为空！"));
        }
        else
        {
            if (this.txtValid.Text.Trim() == this.labValid.Text.Trim())
            {
                //调用函数(输入用户名和密码，返回SqlDataReader类的客户所有信息表)判断用户[this.Username.Value.Trim(),this.Password.Value.Trim()]是否存在,若存在，则：
                Client user = new Client();
                int userID = user.IdentifyUser(Username.Value.ToString(), Password.Value.ToString());
                if (userID > 0)
                {
                    user.GetUser(userID);
                    Session["User"] = user;
                    if (user.roleID == 2) 
                    { 
                        labVIP.Visible = true; 
                    }

                    labUserName.Text = user.userName;
                    if (user.lastLog == user.regTime)
                    {
                        labLastLog.Text = "这是您第一次登录";
                    }
                    else
                    {
                        labLastLog.Text = "上次登录时间为：" + user.lastLog.ToString();
                    }
                    //跳转到当前请求的虚拟路径
                    this.tablogin.Visible = false;
                    this.tabafterlogin.Visible = true;
                }
                else
                {
                    Username.Value = "";
                    labError.Visible = true;
                }
            }
            else
            {
                Response.Write(ccObj.MessageBoxPage("请正确输入验证码！"));
            }
        }
    }
    protected void Register_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }
    protected void Logout_Click(object sender, EventArgs e)
    {
        //清空Session对象
        Session["User"] = null;
        this.tablogin.Visible = true;  //显示用户登录界面
        this.tabafterlogin.Visible = false; //隐藏用户欢迎界面
        Response.Write(ccObj.MessageBox("谢谢您的惠顾，欢迎下次光临！", "Default.aspx"));
    }
    protected void Changeinfo_Click(object sender, EventArgs e)
    {
        Response.Redirect("Userinfo.aspx");
    }
}
